package com.jlanglang.glide.loaderdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.baozi.treerecyclerview.adpater.TreeRecyclerAdapter
import com.baozi.treerecyclerview.item.SimpleTreeItem
import com.baozi.treerecyclerview.item.TreeItem
import com.bumptech.glide.Glide
import com.jlanglang.glide.loaderdemo.loader.UrlModel
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        thread {
            Glide.get(this).clearDiskCache();
        }
        Glide.get(this).clearMemory()
        val treeRecyclerAdapter = TreeRecyclerAdapter();
        treeRecyclerAdapter.itemManager.replaceAllItem(getData())

        rv_content.layoutManager = GridLayoutManager(this, 2)
        rv_content.adapter = treeRecyclerAdapter
    }

    val url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fnimg.ws.126.net" +
            "%2F%3Furl%3Dhttp%253A%252F%252Fdingyue.ws.126.net%252F2021%252F0611" +
            "%252F2e84f869j00qujnx3001nc000hs00hsc.jpg%26thumbnail%3D650x2147483647%26qualit" +
            "y%3D80%26type%3Djpg&refer=http%3A%2F%2" +
            "Fnimg.ws.126.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1634917944" +
            "&t=ef10cf80547fcd4f7e5a588df26c29d9"

    /**
     * 生成100个图片
     */
    private fun getData(): List<TreeItem<*>> {
        return arrayOfNulls<TreeItem<*>>(100).map {
            SimpleTreeItem(R.layout.item_image, 2).apply {
                itemBind = {
                    val urlModel = UrlModel(url);
                    val itemView = it.itemView;
                    val imageView = itemView.findViewById<ImageView>(R.id.iv_content);
                    Glide.with(itemView).load(urlModel).into(imageView)
                }
            }
        }
    }
}